<template>
  <el-drawer
    append-to-body
    destroy-on-close
    :size="size"
    :title="title"
    :visible.sync="drawer"
    @close="close"
  >
    <div v-if="drawer">
      <div v-if="lockList.length > 0" class="lockList">
        <div
          v-for="item in lockList"
          :key="item.id"
          class="lockBox"
          @click="bind(item)"
        >
          <div class="info">
            <div class="name">{{ item.lock_alias }}</div>
            <div class="status">电量：{{ item.electric_quantity }}%</div>
          </div>
          <i class="el-icon-circle-plus-outline"></i>
        </div>
      </div>
      <div v-else class="noLock">
        <el-empty description="无可关联的设备" />
        <div class="tip">提示：关联设备的分组与房间分组需一致</div>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import { lockList, lockBind } from '@/api/device'
export default {
  name: 'lockList',
  data() {
    return {
      size: 380,
      title: '关联房间锁',
      lockList: [],
      drawer: false,
      room_id: '',
      group: [],
      formData: {
        group_id: '',
        status: 'no_bind_room',
      },
    }
  },
  methods: {
    showEdit(row) {
      this.room_id = row.room_id
      this.formData.group_id = row.group_id
      this.getDevice()
      this.drawer = true
    },
    bind(item) {
      const query = {
        lock_id: item.lock_id,
        room_id: this.room_id,
      }
      console.log(query)
      this.$baseConfirm('你确定要关联此锁吗？', null, async () => {
        const { data } = await lockBind(query)
        if (data.is_success) {
          this.$baseMessage('绑定成功', 'success', 'vab-hey-message-success')
          this.$emit('fetch-data')
          this.close()
        }
      })
    },
    close() {
      this.drawer = false
    },
    async getDevice() {
      const { data } = await lockList(this.formData)
      this.lockList = data.items
    },
  },
}
</script>
<style lang="scss" scoped>
.lockList {
  padding: 20px;
  .lockBox {
    padding: 16px;
    background: #f0f7ff;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    &:hover {
      box-shadow: 0 0 12px #ccc;
    }
    .name {
      font-size: 16px;
      color: #000;
      font-weight: bolder;
      line-height: 26px;
    }
    .detail {
      font-size: 14px;
      color: #3f3f3f;
      margin-top: 4px;
    }
    .el-icon-circle-plus-outline {
      font-size: 16px;
      color: #409eff;
    }
  }
}
.noLock {
  text-align: center;
  .tip{
    color:#f5961e
  }
}
</style>